//
//  UIWidgetViewer2.m
//  FloatingWidget
//
//  Created by silent on 10. 3. 11..
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UIWidgetViewer2.h"


@implementation UIWidgetViewer2


- (UIView *)findUIWebDocumentView{
	UIView *webDocumentView = nil;
	NSString *className;
	UIView *scroller = [[self subviews] objectAtIndex:0];
	for(UIView *view in [scroller subviews]){
		className = [NSString stringWithFormat:@"%@", [view class]];
		NSLog(@"found a %@", className);
		if([className isEqualToString:@"UIWebDocumentView"]){ 
			NSLog(@"found the UIWebDocumentView");
			webDocumentView = view;
		}
	}
	return webDocumentView;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code

		CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
		panelView = [[UIView alloc] initWithFrame:rect];

		[self addSubview:panelView];
		
		documentView = [self findUIWebDocumentView];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
	[panelView release];
    [super dealloc];
}

#pragma mark -
- (void)setURL:(NSString *)urlString {
	NSURL *url = [NSURL URLWithString:urlString];
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	[self loadRequest:request];
}

#pragma mark -
CGPoint startLocation;
BOOL isMoved;

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
	isMoved = NO;
	
	NSLog(@"touchesBegan2");
	// Retrieve the touch point
	CGPoint pt = [[touches anyObject] locationInView:self];
	startLocation = pt;
	[[self superview] bringSubviewToFront:self];
	
	//[documentView touchesBegan:touches withEvent:event];
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	[documentView touchesCancelled:touches withEvent:event];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"touchesEnded2");
	if (!isMoved) {
		NSLog(@"touchesEnded2 - not moved");
		[documentView touchesBegan:touches withEvent:event];
		[documentView touchesEnded:touches withEvent:event];
	}
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
	isMoved = YES;
	NSLog(@"touchesMoved2");
	// Move relative to the original touch point
	CGPoint pt = [[touches anyObject] locationInView:self];
	CGRect frame = [self frame];
	frame.origin.x += pt.x - startLocation.x;
	frame.origin.y += pt.y - startLocation.y;
	[self setFrame:frame];
}



@end
